<template>
    <div
        id="design-middle-layout"
        :class="`design-layout-preview page-site-zf ${text_direction}`"
        @click.self="handle_release_selected">

        <!-- 主要布局，中间，最小宽度 375x667 -->
        <div class="main-layout is-app" :class="{ 'is-empty': layouts.length <= 0}">
            <div class="controller-title">
                iphone8屏幕高度------
            </div>

            <!-- 拖拽区域 -->
            <nexted
                :tasks.sync="components"
                @update-layouts="updateLayouts">
            </nexted>

            <!-- 空信息 -->
            <div class="is-empty" v-if="components.length <= 0">
                <img :src="images.emptyImage">
                哎哟，您还没有放置组件哦~ <br/> 赶紧拖动组件到这里设置吧
            </div>
        </div>
    </div>
</template>

<script>
import draggable from 'vuedraggable'
import controller  from './controller.vue';
import emptyImage from '@/resource/images/empty-preview.png';
import nexted from './nested';

export default {
    components: {
        draggable,
        controller,
        nexted
    },
	  props: ["previewData"],
    data () {
        return {
            // 拖拽组件参数
            dragOptions: {
                group: "description",
                disabled: false,
                touchStartThreshold: 5,
                ghostClass: "sortable-ghost",
                dragClass: "sortable-drag",
                filter: ".controller-title, .controller-aside", // 不允许此元素拖拽
            },
            // 当前页面布局信息
            layouts: [],
            images: {
                emptyImage
            }
        };
    },

    computed: {
        // 页面所有组件
        components: {
            get () {
                return this.previewData || this.$store.state.page.components || [];
            },
            set (arr) {
                this.$store.dispatch('design/page_update_layout_v2', arr);
            }
        },

        // 文案方向
        text_direction () {
            const map = ['he'];
            const lang = this.$store.state.page.info.lang || 'en';
            return map.includes(lang) ? 'rtl' : 'ltr';
        }
    },

    methods: {

        // 更新布局数据
        updateLayouts (layouts) {
	        let deleteID = "";

	        // 顶部菜单组件
	        let U_topmenu_length = layouts.filter(item => {
		        return item.component_key === 'U_topmenu'
	        });
	        if(U_topmenu_length.length === 1 && layouts.length > 1 && layouts[0].component_key !== 'U_topmenu') {
	        	let topmenu = {};
		        for(let i = 0;i<layouts.length;i++) {
			        if(layouts[i].component_key === 'U_topmenu') {
				        topmenu = layouts.splice(i,1)[0];
			        }
		        }
		        layouts.unshift(topmenu);
		        this.$message.error('顶部菜单组件只能放在最顶部');
	        }
	        if(U_topmenu_length.length > 1) {
		        // console.log(Number(U_topmenu_length[0].id.slice(1)), Number(U_topmenu_length[1].id.slice(1)))
		        deleteID = Number(U_topmenu_length[0].id.slice(1)) > Number(U_topmenu_length[1].id.slice(1)) ? U_topmenu_length[0].id : U_topmenu_length[1].id;
		        this.$message.error('顶部菜单组件只能放一个');
	        }

	        let U_selfpick_length = layouts.filter(item => {
		        return item.component_key === 'U_selfpick'
	        });
	        if(U_selfpick_length.length > 1) {
		        deleteID = Number(U_selfpick_length[0].id.slice(1)) > Number(U_selfpick_length[1].id.slice(1)) ? U_selfpick_length[0].id : U_selfpick_length[1].id;
		        this.$message.error('社区团购组件只能放一个');
	        }

	        if(this.fun.getKey('page_sort') == 3) {
		        let U_suspendbutton_length = layouts.filter(item => {
			        return item.component_key === 'U_suspendbutton'
		        });
		        if(U_suspendbutton_length.length > 1) {
			        deleteID = Number(U_suspendbutton_length[0].id.slice(1)) > Number(U_suspendbutton_length[1].id.slice(1)) ? U_suspendbutton_length[0].id : U_suspendbutton_length[1].id;
			        this.$message.error('悬浮按钮组件只能放一个');
		        }
          }

	        // 多级选项卡组件
	        // let U_moretabcontrol_length = layouts.filter(item => {
		       //  return item.component_key === 'U_moretabcontrol'
	        // });
	        // if(U_moretabcontrol_length.length === 1 && layouts.length >1) {
		       //  this.$confirm({
			     //    title: '提示',
			     //    cancelText: "保留其余组件",
			     //    okText: "只显示多级选项卡",
			     //    content: '多级选项卡组件只能单独放一个页面其余组件将会删除, 是否继续?' ,
			     //    onOk: () => {
				   //      for (let index = layouts.length - 1; index >= 0 ; index--) {
					 //        // 逆向循环
					 //        if(layouts[index].component_key !== 'U_moretabcontrol'){
						//         layouts.splice(index, 1);
					 //        }
				   //      }
			     //    },
			     //    onCancel: () => {
				   //      for (let index = layouts.length - 1; index >= 0 ; index--) {
					 //        // 逆向循环
					 //        if(layouts[index].component_key === 'U_moretabcontrol'){
						//         layouts.splice(index, 1);
					 //        }
				   //      }
			     //    }
		       //  });
	        // }
          // if(U_moretabcontrol_length.length > 1) {
		       //  deleteID = Number(U_moretabcontrol_length[0].id.slice(1)) > Number(U_moretabcontrol_length[1].id.slice(1)) ? U_moretabcontrol_length[0].id : U_moretabcontrol_length[1].id;
	        //   this.$message.error('多级选项卡组件只能放一个');
	        // }

	        for(let i = 0;i<layouts.length;i++) {
		        if(layouts[i].id === deleteID) {
			        layouts.splice(i,1);
		        }
	        }

          this.$store.dispatch('design/page_update_layout_v2', layouts);
        },

        /**
         * 开始拖拽
         */
        handle_drag_start () {
            this.$store.state.design.preview_in_drag = true;
        },

        /**
         * 结束拖拽
         */
        handle_drag_end () {
            this.$store.state.design.preview_in_drag = false;
        },

        /**
         * 释放组件选中效果
         */
        handle_release_selected () {
            this.$store.dispatch('design/form_close', {num : '2'});
        }
    }
}
</script>

<style lang="less" scoped>

.design-layout-preview {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 100%;
    padding-top: 114px;
    padding-bottom: 100px;
    overflow-y: scroll;

    // 主要预览布局
    .main-layout {
        position: relative;
        margin: 0 auto;
        width: 375px;
        min-height: 667px;
        background: #fff;
        box-shadow:-10px 20px 30px 0px rgba(192,197,205,0.8);
        // 为空
        &.is-empty {
            .is-empty {
                display: block;
            }
        }
        // 空数据的样式
        .is-empty {
            position: absolute;
            left: 0px;
            top: 240px;
            right: 0px;
            display: none;
            text-align: center;
            color: rgba(92, 162, 232, 0.46);
            font-size: 14px;
            img {
                width: 190px;
                height: 125px;
                margin: 0 auto;
                display: block;
            }
        }
    }

    // 拖拽区域
    .dragArea {
        min-height: 667px;
        > span {
            display: block;
            min-height: 667px;
        }
    }
}

// 拖拽效果
.flip-list-move {
    transition: transform 0.5s;
}

// 拖拽组件容器
.list-group-item {
    position: relative;
    &.is-hover {
        &:before {
            position: absolute;
            content: " ";
            left: 0px;
            bottom: 0px;
            top: 0px;
            right: 0px;
            border: dashed 2px #29ba9c;
        }
        .component-controller {
            display: block;
        }
    }

    // 选中打开表单中
    &.is-active {
        &::before {
            position: absolute;
            content: " ";
            left: 0px;
            bottom: 0px;
            top: 0px;
            right: 0px;
            border: solid 3px #29ba9c;
        }
    }
}
</style>

<style lang="less">

// 拖拽时的样式
.design-layout-preview {
    .main-layout {
        .dragArea {
            > .sortable-ghost {
                position: relative;
                list-style: none;
                width: 100%;
                height: 100px;
                line-height: 100px;
                text-align: center;
                background:rgba(64,158,255,0.3) !important;
                cursor: move;

                i,
                p,
                .component-box,
                > .dragArea {
                    display: none;
                }

                > * {
                    display: none;
                }

                &:before {
                    position: absolute;
                    content: " ";
                    left: 0px;
                    bottom: 0px;
                    top: 0px;
                    right: 0px;
                    border: 3px solid #29ba9c;
                }
                &:after {
                    top: 0px;
                    content: "我要在这里";
                }
            }
        }
    }
}

.controller-title {
    position: absolute;
    top: 655px;
    right: 100%;
    word-break: keep-all;
    white-space: nowrap;
}

// 测试信息
.debuger-info {
    background: #333;
    color: #fff;
    padding: 10px;
    label {
        font-weight: bold;
    }
    p {
        margin: 0px;
        margin-bottom: 10px;
    }
}
</style>
